package com.example.myapplication1.ui.follow;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.example.myapplication1.R;

import java.util.HashMap;
import java.util.Map;

public class FollowFragment extends Fragment {
    private TextView totalPriceText;
    private Button submitOrderButton;
    private Map<String, Double> prices;
    private Map<String, Integer> quantities;
    private Map<String, TextView> quantityTexts;
    private double totalPrice = 0.0;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_follow, container, false);

        // 初始化价格表
        prices = new HashMap<>();
        prices.put("cat_food", 0.20);
        prices.put("freeze_dried", 0.30);
        prices.put("cat_treats", 0.50);

        // 初始化数量表





        
        quantities = new HashMap<>();
        quantities.put("cat_food", 0);
        quantities.put("freeze_dried", 0);
        quantities.put("cat_treats", 0);

        // 初始化数量显示文本Map
        quantityTexts = new HashMap<>();
        quantityTexts.put("cat_food", view.findViewById(R.id.cat_food_quantity));
        quantityTexts.put("freeze_dried", view.findViewById(R.id.freeze_dried_quantity));
        quantityTexts.put("cat_treats", view.findViewById(R.id.cat_treats_quantity));

        // 初始化视图
        totalPriceText = view.findViewById(R.id.total_price);
        submitOrderButton = view.findViewById(R.id.submit_order);

        // 设置加号和减号按钮点击事件
        setupButtons(view, R.id.cat_food_add, R.id.cat_food_minus, "cat_food");
        setupButtons(view, R.id.freeze_dried_add, R.id.freeze_dried_minus, "freeze_dried");
        setupButtons(view, R.id.cat_treats_add, R.id.cat_treats_minus, "cat_treats");

        // 设置提交订单按钮点击事件
        submitOrderButton.setOnClickListener(v -> {
            if (totalPrice > 0) {
                Toast.makeText(requireContext(), "订单提交成功！", Toast.LENGTH_SHORT).show();
                // TODO: 跳转到订单确认页面
            } else {
                Toast.makeText(requireContext(), "请先选择商品", Toast.LENGTH_SHORT).show();
            }
        });

        return view;
    }

    private void setupButtons(View view, int addButtonId, int minusButtonId, String productKey) {
        ImageButton addButton = view.findViewById(addButtonId);
        ImageButton minusButton = view.findViewById(minusButtonId);

        addButton.setOnClickListener(v -> updateQuantity(productKey, true));
        minusButton.setOnClickListener(v -> updateQuantity(productKey, false));
    }

    private void updateQuantity(String productKey, boolean isAdd) {
        int currentQuantity = quantities.get(productKey);
        
        if (isAdd) {
            // 增加数量
            quantities.put(productKey, currentQuantity + 1);
            totalPrice += prices.get(productKey);
            updateDisplay(productKey, currentQuantity + 1);
            Toast.makeText(requireContext(), 
                "已添加" + getProductName(productKey), 
                Toast.LENGTH_SHORT).show();
        } else if (currentQuantity > 0) {
            // 减少数量
            quantities.put(productKey, currentQuantity - 1);
            totalPrice -= prices.get(productKey);
            updateDisplay(productKey, currentQuantity - 1);
            Toast.makeText(requireContext(), 
                "已减少" + getProductName(productKey), 
                Toast.LENGTH_SHORT).show();
        }
    }

    private void updateDisplay(String productKey, int quantity) {
        // 更新数量显示
        TextView quantityText = quantityTexts.get(productKey);
        if (quantityText != null) {
            quantityText.setText(String.valueOf(quantity));
        }
        // 更新总价显示
        updateTotalPrice();
    }

    private String getProductName(String productKey) {
        switch (productKey) {
            case "cat_food":
                return "营养猫粮";
            case "freeze_dried":
                return "小猫冻干";
            case "cat_treats":
                return "营养猫条";
            default:
                return "";
        }
    }

    private void updateTotalPrice() {
        totalPriceText.setText(String.format("总计: ¥ %.2f", totalPrice));
    }
} 